﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using System.Drawing;
using OpenTK.Graphics;
using Common;

namespace T11_2DFonts {
    class T11_2DFontsGameWindow : GameWindow {
        public T11_2DFontsGameWindow() {
            this.Load += T11_2DFontsGameWindow_Load;
            this.Resize += T11_2DFontsGameWindow_Resize;
            this.RenderFrame += T11_2DFontsGameWindow_RenderFrame;
            this.Unload += T11_2DFontsGameWindow_Unload;
            this.Width = 1280;
            this.Height = 720;
            this.Title = "T11_2DFontsGameWindow";

        }


        private void T11_2DFontsGameWindow_RenderFrame(object sender, FrameEventArgs e) {
            GL.ClearColor(.1f, .1f, .99f, .9f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            Render();

            SwapBuffers();
            ProcessEvents();
        }
        Shader shader, textShader;
        Vao vao;
        Texture2D texture;
        ModelMesh mesh;
        Vbo<Vector3> vertexBuffer, normalBuffer;
        Vbo<Vector2> uvBuffer;
        Vbo<ushort> indexBuffer;
        VertexAttribManager attribs = new VertexAttribManager();
        Font2D fontDrawer;
        private void T11_2DFontsGameWindow_Load(object sender, EventArgs e) {

            VSync = VSyncMode.Off;
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);
            GL.Enable(EnableCap.CullFace);

            shader = new Shader("shader.vs", "shader.fs");
            textShader = new Shader("text.vs", "text.fs");
            vao = new Vao();
            texture = Texture2D.Load("uvmap.dds");
            mesh = MeshLoader.LoadObjIndexed("suzanne.obj");
            vertexBuffer = new Vbo<Vector3>(mesh.Vertices);
            uvBuffer = new Vbo<Vector2>(mesh.Uvs);
            normalBuffer = new Vbo<Vector3>(mesh.Normals);
            indexBuffer = new Vbo<ushort>(BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticDraw, mesh.Indices);

            attribs.AddAttrib(3, vertexBuffer);
            attribs.AddAttrib(2, uvBuffer);
            attribs.AddAttrib(3, normalBuffer);

            fontDrawer=new Font2D("Holstein.DDS", textShader, "myTextureSampler");

        }

        private void Render() {
            shader.Use();
            InputControls.computeMatricesFromInput(this);
            FpsCounter.CountFps();
            var model = Matrix4.Identity;
            var mvp = model * InputControls.ViewMatrix * InputControls.ProjectionMatrix;
            shader.SetMatrix4("M", ref model);
            shader.SetMatrix4("V", ref InputControls.ViewMatrix);
            shader.SetMatrix4("MVP", ref mvp);

            Vector3 lightPos = new Vector3(4, 4, 4);
            shader.SetVector3("LightPosition_worldspace", lightPos);
            texture.Active(TextureUnit.Texture1, shader, "myTextureSampler");
            attribs.Enable();
            GL.DrawElements(BeginMode.Triangles, mesh.Indices.Length, DrawElementsType.UnsignedShort, 0);
            attribs.Disable();
            string text = $"{InputControls.getTime():0.00}";
            fontDrawer.printText2d(text, 10, 500, 60);

        }

        private void T11_2DFontsGameWindow_Resize(object sender, EventArgs e) {
            GL.Viewport(0, 0, Width, Height);
        }



        private void T11_2DFontsGameWindow_Unload(object sender, EventArgs e) {
            GlDisposableObjectBase.DisposeObjects(vao,shader, texture, textShader, vertexBuffer, uvBuffer, normalBuffer, indexBuffer,fontDrawer,mesh);
        }

    }
}
